import stoarge from "@/utils/stoarge.js";
import st from "@/utils/showtoast.js";
import Vue from "vue";

export default {
	/**
	 * 获取登录状态
	 */
	loginStatus() {
		return stoarge.getToken() === "" ? false : true;
	},
	/**
	 * 判断是否登录
	 */
	isLogin() {
		if (stoarge.getToken() === "") {
			uni.navigateTo({
				url: `/pages/login/login`,
			});
			return false;
		}
		return true;
	},

	/**
	 * 获取价格整数部分
	 */
	getPriceInt(price) {
		return price.split(".")[0];
	},
	/**
	 * 获取价格小数部分
	 */
	getPriceDecimal(price) {
		if (price.indexOf(".") === -1) {
			return "00";
		}
		return price.split(".")[1];
	},
	/**
	 * 根据时间获取xxx前
	 */
	getTimeAgo(time) {
		const systemInfo = uni.getSystemInfoSync();
		var timeSpec = time;
		if (systemInfo.osName === "ios") {
			timeSpec = time.replace(/-/gi, "/");
		}

		const now = new Date().getTime();
		const before = new Date(timeSpec).getTime();
		const diff = now - before;

		var text = "";

		if (diff < 60) {
			text = "刚刚";
		} else if (diff < 3600) {
			text = Math.floor(diff / 60) + "分钟前";
		} else if (diff < 86400) {
			text = Math.floor(diff / 3600) + "小时前";
		} else if (diff < 604800) {
			text = Math.floor(diff / 86400) + "天前";
		} else {
			text = timeSpec.split(" ")[0];
		}

		return text;
	},
	/**
	 * 预览图片
	 * @param {Array} imageArray 图片数组
	 * @param {Number} current 当前图片索引
	 */
	onPreviewImage(imageArray, current) {
		let imgList = [];
		imageArray.forEach((item) => {
			let url = item;
			if (!item.startsWith("http")) {
				url = Vue.prototype.$getImageURL(item);
			}
			imgList.push(url);
		});

		uni.previewImage({
			urls: imgList,
			current: current,
		});
	},
	/**
	 * 选择图片
	 */
	onChooseImage(count, callBack) {
		uni.chooseImage({
			count: count,
			success: (res) => {
				if (callBack) {
					callBack(res.tempFilePaths);
				}
			},
		});
	},
	/**
	 * 判断是否存在值
	 */
	handleValue(value, defaultVal = "") {
		if (value === null || value === undefined) {
			return defaultVal;
		}
		return value;
	},
	/**
	 * 根据图片的字符串，返回图片的数组
	 */
	getImageArray(images) {
		if (images === undefined || images === null || images === "") {
			return [];
		} else {
			return images.split(",");
		}
	},
	/**
	 * 暂无开放的提示
	 */
	onActivityAlert(item) {
		if (item.name === "各类活动") {
			st.show("暂未开放活动专区");
			return false;
		}
		return true;
	},
	/**
	 * 拨打电话
	 */
	onCallPhone(phone) {
		uni.makePhoneCall({
			phoneNumber: phone.toString(),
		});
	},
	/**
	 * 判断表单列表正则 旧的
	 */
	judgeFormList(list, dataInfo, require = false) {
		for (let i = 0; i < list.length; i++) {
			const item = list[i];

			var isMust = require;
			if (require === false) {
				isMust = item.require;
			}
			if (isMust && dataInfo[item.key] === "") {
				st.show(item.tips);
				return false;
			}
		}
		return true;
	},
	/**
	 * 判断表单列表正则 新的
	 */
	judgeFormData(list, dataInfo) {
		for (let i = 0; i < list.length; i++) {
			const item = list[i];
			const value = dataInfo[item.key];

			var isMust = true;
			if (item.require !== undefined) {
				isMust = item.require;
			}
			var isShow = true;
			if (item.isShow !== undefined) {
				isShow = item.isShow;
			}
			if (isMust && isShow && ((typeof value === 'string' && value === "") || (typeof value === "object") && value.length === 0)) {
				st.show(item.tips);
				return false;
			}
		}
		return true;
	},
	/**
	 * 支付
	 */
	onPayFunction(data, callBack) {
		uni.requestPayment(
			Object.assign(data, {
				success: (res) => {
					st.success("支付成功", () => {
						callBack & callBack(1);
					});
				},
				fail: (res) => {
					st.fail("支付失败", () => {
						callBack & callBack(0);
					});
				},
			})
		);
	},
	/**
	 * 获取银行卡后四位
	 */
	getBankCardNumber(str) {
		str = str.replace(/\s+/g, "");
		return str.substring(str.length - 4);
	},
	/**
	 * 获取银行卡类型
	 */
	getBankType(index) {
		var bankType = ["储蓄卡", "信用卡"];
		return bankType[index];
	},
	/**
	 * 打开地图获取地址之后取省市区街道
	 */
	onHandleAddress(address) {
		var reg = "(?<province>[^省]+省|.+自治区|[^澳门]+澳门|北京|重庆|上海|天津|台湾|[^香港]+香港|[^市]+市)?(?<city>[^自治州]+自治州|[^特别行政区]+特别行政区|[^市]+市|.*?地区|.*?行政单位|.+盟|市辖区|[^县]+县)(?<county>[^县]+县|[^市]+市|[^镇]+镇|[^区]+区|[^乡]+乡|.+场|.+旗|.+海域|.+岛)?(?<address>.*)"
		var location = address.match(reg);
		
		if (location !== undefined && location !== null) {
			var groups = location.groups;
			var province = groups.province === undefined ? groups.city : groups.province;
			var city = groups.city;
			var district = groups.county;
			var street = groups.address;
			return [province, city, district, street];
		} else {
			return [];
		}
	},
	/**
	 * 退出登录
	 */
	onLogout() {
		uni.showModal({
			title: "提示",
			content: "确定要退出登录么?",
			confirmText: "确认",
			confirmColor: "#e56d00",
			cancelColor: "#999999",
			cancelText: "取消",
			success: (res) => {
				if (res.confirm) {
					Vue.prototype.$store.dispatch("chat/logout");
					stoarge.logout();
					Vue.prototype.$st.show("退出成功");
					uni.navigateBack();
				}
			},
		});
	},
	/**
	 * 获取上一个页面栈
	 */
	prePageRoute() {
		let pages = getCurrentPages();
		let prePage = pages[pages.length - 2];
		return prePage;
	},
	/**
	 * 获取上一个页面栈
	 */
	prePage() {
		let pages = getCurrentPages();
		let prePage = pages[pages.length - 2];
		return prePage.$vm;
	},
	/**
	 * 赋值
	 */
	copy(content) {
		// 复制内容，必须字符串，数字需要转换为字符串
		content = typeof content === "string" ? content : content.toString();
		
		uni.setClipboardData({
			data: content,
			success: function () {
				st.success("复制成功");
			},
			fail: function () {
				st.fail("复制失败");
			},
		});
	},
	/**
	 * 修改标题栏文字
	 */
	title(text = "", setbar = {}) {
		if (text) {
			uni.setNavigationBarTitle({
				title: text
			})
		}
		if (JSON.stringify(setbar) != "{}") {
			uni.setNavigationBarColor(setbar);
		}
	},
	/**
	 * 获取分享信息
	 */
	getShareInfo(dataInfo) {
		var obj = {
			title: "",
			shareImg: Vue.prototype.$getImageURL(dataInfo.wechat_share_img),
		}
		
		let name = dataInfo.title;
		if (dataInfo.is_bonus == 1) {
			if (dataInfo.bonus.type == 0) {
				name = "给您发了一个红包,快来领取吧";
			} else if (dataInfo.bonus.type == 1) {
				name = "给您发了一个优惠券,快来领取吧";
			}
		}
		if (dataInfo.type === "3") { //政务公告
			if (dataInfo.ReceiveEnd == 1) { //领取光了
				if (dataInfo.user_auth == 1) {
					obj.title = `【${dataInfo.auth_info.auth_name}】${dataInfo.title}`;
				} else {
					obj.title = `【百焕商圈】${dataInfo.title}`;
				}
			} else {//没有领取光
				if (dataInfo.user_auth == 1) {
					obj.title = `【${dataInfo.auth_info.auth_name}】${name}`;
				} else {
					obj.title = `【百焕商圈】${name}`;
				}
			}
		} else {
			if (dataInfo.ReceiveEnd == 1) { //领取光了
				if (dataInfo.is_shop === 1) { //是店铺
					obj.title = `【${dataInfo.shop.storename}】${dataInfo.title}`;
					obj.shareImg = Vue.prototype.$getImageURL(dataInfo.shop.avatar);
				} else {
					obj.title = `【${dataInfo.user.nickname}】${dataInfo.title}`;
					obj.shareImg = Vue.prototype.$getImageURL(dataInfo.user.avatar);
				}
			} else { //没有领取光
				if (dataInfo.is_shop === 1) { //是店铺
					obj.title = `【${dataInfo.shop.storename}】${name}`;
				} else {
					obj.title = `【${dataInfo.user.nickname}】${name}`;
				}
			}
		}
		return obj;
	},
	/**
	 * 判断
	 */
	judgeDataInfo() {
		if (dataInfo.ReceiveEnd == 1) { //领取光了
			
		} else {
			
		}
	},
};
